package com.example.warehousewms.controller;

import com.example.warehousewms.bean.StockInOrder;
import com.example.warehousewms.bean.vo.StockInOrderVo;
import com.example.warehousewms.common.R;
import com.example.warehousewms.service.StockInOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StockInOrderController {

    @Autowired
    private StockInOrderService stockInOrderService;

    /**
     * 创建入库单
     */
    @PostMapping("/stockInOrder")
    public R createStockInOrder(@RequestBody StockInOrderVo stockInOrderVo) {

        stockInOrderService.addStockInOrder(stockInOrderVo);

        return R.ok("创建成功");
    }

    /**
     * 编辑入库单
     */
    @PutMapping("/stockInOrder/{id}")
    public R updateStockInOrder(@RequestBody StockInOrderVo stockInOrderVo, @PathVariable Long id) {

        StockInOrder stock = stockInOrderService.getStockInOrderById(id);
        if ("COMPLETED".equals(stock.getOrderStatus()) || "CANCELED".equals(stock.getOrderStatus())){
            return R.error("修改失败，该入库单已结束");
        }
        stockInOrderService.updateStockInOrder(stockInOrderVo,id);

        return R.ok("修改成功");
    }

    /**
     * 完成入库
     */
    @PutMapping("/stockInOrder/finish/{id}")
    public R finishStockInOrder(@PathVariable Long id) {

        stockInOrderService.finishStockInOrder(id);

        return R.ok("完成入库成功");
    }

}
